<?php

class LoginController extends Controller
{
	public $defaultAction = 'login';
	public $layout='leftmenu';

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{		

		if(ApplicationSessions::run()->read('type')!="admin"){
			$model=new UserLogin;
			// collect user input data
			
			if(isset($_POST['UserLogin']))
			{
				
				$model->attributes=$_POST['UserLogin'];
				// validate user input and redirect to previous page if valid
				if($model->validate()) 
				{
					
					$this->lastViset();
					if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false)
						$this->redirect(Yii::app()->controller->module->returnUrl);
					else
						$this->redirect(ApplicationConfig::getURL('', 'admin', 'profile'));
				}
			}
			$this->render('/cpanel/login',array('model'=>$model));
		}
		else
		{
			$this->redirect(ApplicationConfig::getURL('', 'admin', 'profile'));
		}
	}
	
	private function lastViset() {
		$lastVisit = Admin::model()->notsafe()->findByPk(Yii::app()->user->id);
		$lastVisit->lastvisit = time();
		$lastVisit->save();
	}

}